namespace Caculator
{
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            richTextBox1.Text += "1";
        }

        private void button2_Click(object sender, EventArgs e) {
            richTextBox1.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e) {
            richTextBox1.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e) {
            richTextBox1.Text += "4";
        }
        private void button5_Click(object sender, EventArgs e) {
            richTextBox1.Text += "5";
        }
        private void button6_Click(object sender, EventArgs e) {
            richTextBox1.Text += "6";
        }
        private void button7_Click(object sender, EventArgs e) {
            richTextBox1.Text += "7";
        }
        private void button8_Click(object sender, EventArgs e) {
            richTextBox1.Text += "8";
        }
        private void button9_Click(object sender, EventArgs e) {
            richTextBox1.Text += "9";
        }
        private void button10_Click(object sender, EventArgs e) {
            richTextBox1.Text += "+";
        }
        private void button11_Click(object sender, EventArgs e) {
            richTextBox1.Text += "-";
        }
        private void button12_Click(object sender, EventArgs e) {
            richTextBox1.Text += "*";
        }
        private void button13_Click(object sender, EventArgs e) {
            richTextBox1.Text += "/";
        }
        private void button14_Click(object sender, EventArgs e) {
            int result = Evaluate(richTextBox1.Text);
            richTextBox1.Text = result.ToString();
        }
        private void button15_Click(object sender, EventArgs e) {
            richTextBox1.Text = "";
        }
        public static int Evaluate(string expression) {
            Stack<int> numbers = new Stack<int>();
            Stack<char> operators = new Stack<char>();

            for (int i = 0; i < expression.Length; i++) {
                char c = expression[i];

                if (char.IsDigit(c)) {
                    int num = 0;
                    while (i < expression.Length && char.IsDigit(expression[i])) {
                        num = num * 10 + (expression[i] - '0');
                        i++;
                    }
                    i--;
                    numbers.Push(num);
                }

                else if (c == '+' || c == '-' || c == '*' || c == '/') {
                    while (operators.Count > 0 && HasPrecedence(c, operators.Peek())) {
                        numbers.Push(ApplyOp(operators.Pop(), numbers.Pop(), numbers.Pop()));
                    }
                    operators.Push(c);
                }
                else {
                    throw new ArgumentException("Invalid character in expression: " + c);
                }
            }

            while (operators.Count > 0) {
                numbers.Push(ApplyOp(operators.Pop(), numbers.Pop(), numbers.Pop()));
            }

            if (numbers.Count != 1) {
                throw new ArgumentException("Invalid expression format");
            }

            return numbers.Pop();
        }

        private static bool HasPrecedence(char op1, char op2) {
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            return true;
        }

        private static int ApplyOp(char op, int b, int a) {
            switch (op) {
                case '+': return a + b;
                case '-': return a - b;
                case '*': return a * b;
                case '/':
                if (b == 0)
                    throw new DivideByZeroException("Division by zero");
                return a / b;
                default:
                throw new ArgumentException("Invalid operator: " + op);
            }
        }

    }
}
